package lol.clann.minecraft.springboot.plugin.bukkit.impl;

import lol.clann.minecraft.springboot.api.annotation.compatibility.CompatibilityChecked;
import lol.clann.minecraft.springboot.api.bukkit.utils.LocationUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.NBTUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.TileEntityUtils;
import lol.clann.minecraft.springboot.api.bukkit.utils.WorldUtils;
import lol.clann.minecraft.springboot.bukkit.utils.BukkitUtils;
import net.minecraft.server.v1_7_R4.CrashReportSystemDetails;
import net.minecraft.server.v1_7_R4.NBTTagCompound;
import net.minecraft.server.v1_7_R4.TileEntity;
import org.bukkit.Location;
import org.bukkit.World;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author pyz
 * @date 2019/5/13 12:36 AM
 */
@Service
@CompatibilityChecked
public class TileEntityUtilsImpl_all implements TileEntityUtils {

    @Autowired
    private LocationUtils locationUtils;
    @Autowired
    private WorldUtils worldUtils;
    @Autowired
    private NBTUtils nbtUtils;

    @Override
    public Location getLocation(Object tileEntity) {
        if (BukkitUtils.getBukkitVersionNumber() < 10800) {
            TileEntity te = (TileEntity) tileEntity;
            net.minecraft.server.v1_7_R4.World nmsWorld = te.getWorld();
            World world = worldUtils.toBukkit(nmsWorld);
            return new Location(world, te.x, te.y, te.z);
        } else {
            net.minecraft.server.v1_8_R3.TileEntity te = (net.minecraft.server.v1_8_R3.TileEntity) tileEntity;
            net.minecraft.server.v1_8_R3.World nmsWorld = te.getWorld();
            World world = worldUtils.toBukkit(nmsWorld);
            return locationUtils.toLocation(world, te.getPosition());
        }
    }

    @Override
    public Object toNBTTagCompound(Object nmsTileEntity) {
        Object tag = nbtUtils.newNBTTagCompound();
        if (BukkitUtils.getBukkitVersionNumber() < 10900) {
            TileEntity te = (TileEntity) nmsTileEntity;
            te.b((NBTTagCompound) tag);
        } else {
            net.minecraft.server.v1_9_R2.TileEntity te = (net.minecraft.server.v1_9_R2.TileEntity) nmsTileEntity;
            te.save((net.minecraft.server.v1_9_R2.NBTTagCompound) tag);
        }
        return tag;
    }

    @Override
    public void applyNBTTagCompound(Object nmsTileEntity, Object nbtTagCompound) {
        if (BukkitUtils.getBukkitVersionNumber() < 10900) {
            TileEntity te = (TileEntity) nmsTileEntity;
            te.a((CrashReportSystemDetails) nbtTagCompound);
        } else {
            net.minecraft.server.v1_9_R2.TileEntity te = (net.minecraft.server.v1_9_R2.TileEntity) nmsTileEntity;
            te.save((net.minecraft.server.v1_9_R2.NBTTagCompound) nbtTagCompound);
        }
    }

}
